package com.ls.controller;

import com.ls.entity.User;
import com.ls.service.UserService;
import com.ls.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService service;

    @GetMapping("user/login")
    public Map<String, Object> login(String username, String password) {
        log.info("username:[{}]", username);
        log.info("pwd:[{}]", username);
        Map<String, Object> map = new HashMap<>();
        try {
            User user = service.login(username, password);

            Map<String,String> payload=new HashMap<>();
            payload.put("id",String.valueOf(user.getId()));
            payload.put("name",user.getUsername());
            //生成jwt令牌
            String token = JwtUtils.getToken(payload);
            map.put("status",0);
            map.put("msg","登录成功");
            map.put("token",token);
            return map;
        } catch (Exception e) {
            map.put("status",1);
            map.put("msg","登录失败");
            return map;
        }
    }

}
